package com.lhq.shiro.realms;

import com.lhq.entity.Perms;
import com.lhq.entity.Role;
import com.lhq.entity.User;
import com.lhq.service.UserService;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * @ClassName CustomerRealm
 * @Description:类描述
 * @Author Tedu
 * @Date 2021/7/10 21:53
 * @Version V1.0
 **/
public class CustomerRealm extends AuthorizingRealm {
    @Autowired
    private UserService userService;
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        //获取身份信息
       String primaryPrincipal = (String) principals.getPrimaryPrincipal();
        System.out.println("调用授权验证: "+primaryPrincipal);
       //根据主身份信息获取角色和权限信息
        User user = userService.findRolesByUsername(primaryPrincipal);
        //授权角色信息
        if (!CollectionUtils.isEmpty(user.getRoles())){
            SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
            for (Role role : user.getRoles()) {
                simpleAuthorizationInfo.addRole(role.getName());
                //权限信息
                List<Perms> perms = userService.findPermsByRoleId(role.getId());
                if (!CollectionUtils.isEmpty(perms)){
                    for (Perms perm : perms) {
                        simpleAuthorizationInfo.addStringPermission(perm.getName());
                        simpleAuthorizationInfo.addStringPermission(perm.getUrl());
                    }

                }


            }
            return simpleAuthorizationInfo;
        }

        /*if ("xiaochen".equals(primaryPrincipal)){
            SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
            simpleAuthorizationInfo.addRole("user");
            simpleAuthorizationInfo.addRole("admin");
            //simpleAuthorizationInfo.addStringPermission("user:*:*");
            simpleAuthorizationInfo.addStringPermission("user:add:*");
            simpleAuthorizationInfo.addStringPermission("user:delete:*");
            return simpleAuthorizationInfo;

        }*/

        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        System.out.println("=========================");
        String principal = (String) token.getPrincipal();
       // SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo();
        User user = userService.findByUserName(principal);
        if (!ObjectUtils.isEmpty(user)){
            return new SimpleAuthenticationInfo(
                    user.getUsername(),user.getPassword(),
                    ByteSource.Util.bytes(user.getSalt()),this.getName());
        }
        /*if ("xiaochen".equals(principal)){
            return new SimpleAuthenticationInfo(
                    principal, "123",this.getName());

        }*/


        return null;

    }

    public static void main(String[] args) {
        int i=0;
       int  l=i++ + i;
        System.out.println(l);


    }
}
